/*==============================================================================
FILE NAME: Figure_1.do
CREATED: 1 July 2025
==============================================================================*/


/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"
	global point_estimates "$figures/point_estimates" // Define global paths for replication package
} 
*/

set scheme modern

import delimited using "$processed_data/citizen_complaints_clean.csv", clear

* Create indicator variable
gen n = 1

* Loop through each q13 variable
foreach y in better_information better_job ej bad_info basic_rights undue_influence {
cap drop x
gen x = 1 if `y' == "Strongly agree"
replace x = 2 if `y' == "Agree"
replace x = 3 if `y' == "Neither agree nor disagree"
replace x = 4 if `y'  == "Disagree"
replace x = 5 if `y' == "Strongly disagree"


* Set custom file names
local filename ""
if "`y'" == "better_information" local filename "Figure_1_Panel_A"
else if "`y'" == "ej" local filename "Figure_1_Panel_B"
else if "`y'" == "basic_rights" local filename "Figure_1_Panel_C"
else if "`y'" == "bad_info" local filename "Figure_1_Panel_D"
else if "`y'" == "undue_influence" local filename "Figure_1_Panel_E"

* Calculate frequencies for point estimates
preserve
collapse (count) count=n, by(`y')
gen total = _N
egen total_responses = sum(count)
gen proportion = count / total_responses
gen percentage = proportion * 100
	

export delimited using "$point_estimates/Point_estimates_`filename'.csv", replace
	restore

* Create horizontal bar graph based on survey
graph set window fontface "Times New Roman"
graph hbar (sum) n, over(`y', sort(x) label(labsize(large))) ///
    ytitle("# of Responses", size(large)) ///
    ylab(, nogrid labsize(large)) ///
    bar(1, color(black)) ///
    graphregion(color(white)) ///
    plotregion(color(white))

* Graph export "`y'.pdf", replace
graph export "$figures/`filename'.pdf", replace

}